package com.ly.lw.styledo.fragment;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.ly.lw.styledo.BaseApp;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.MyLogoAdapter;
import com.ly.lw.styledo.bean.Logo;
import com.ly.lw.styledo.widget.ListViewForScrollView;

import java.util.ArrayList;
import java.util.List;


public class MyShopFragment extends Fragment {
    private ListViewForScrollView listViewForScrollView;
    private BitmapUtils bitmapUtils;
    private MyLogoAdapter myLogoAdapter;
    private List<Logo> listBrandLogo = new ArrayList<>();
    private DbUtils db;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_shop, container, false);
        initView(view);
        loadData();

        return view;
    }



    private void initView(View view) {
        listViewForScrollView = (ListViewForScrollView) view.findViewById(R.id.my_list_view);
        myLogoAdapter = new MyLogoAdapter(listBrandLogo,getActivity());
        listViewForScrollView.setAdapter(myLogoAdapter);
        listViewForScrollView.setFocusable(false);

        setListViewHeightBasedOnChildren(listViewForScrollView);

    }


    private void loadData()  {
        if(listBrandLogo != null) {
            listBrandLogo.clear();
        }

        db = BaseApp.getInstance().getDbUtils();
        try {
            List<Logo> logoList = db.findAll(Logo.class);

            if(logoList != null && logoList.size()>0) {
                listBrandLogo.addAll(logoList);
                myLogoAdapter.notifyDataSetChanged();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }


    }


    private void setListViewHeightBasedOnChildren(ListViewForScrollView listViewForScrollView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listViewForScrollView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listViewForScrollView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listViewForScrollView.getLayoutParams();
        params.height = totalHeight + (listViewForScrollView.getDividerHeight() * (listAdapter.getCount() - 1)) ;
        // listView.getDividerHeight()获取子项间分隔符占用的高度   + (staggeredGridView. * (listAdapter.getCount() - 1))
        // params.height最后得到整个ListView完整显示需要的高度
        listViewForScrollView.setLayoutParams(params);
    }


    @Override
    public void onStart() {
        super.onStart();
        loadData();
    }



}
